<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of shippingcom
 *
 * @author hassanali
 */
class cases extends admin_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('admin/cases_model', 'cass');
        $this->load->model('admin/case_replay_model', 'replay');
    }

    public function index() {
        $data['result'] = $this->cass->filter();
        $data['pagination'] = $this->admin_pagination('index', $this->cass->filter(true), 10);
        $this->themes->render('cases/index', $data);
    }

    public function view($id = FALSE) {
        if (is_numeric($id)) {
            $falid_id = $this->cass->is_found(array('id' => $id), array(), 'row', __CLASS__);
            $data['result'] = $falid_id;
            $data['reply'] = $this->replay->get(array('case_id' => $id));
            $this->themes->render('cases/view', $data);
        } else {
            redirect(__CLASS__);
        }
    }

    public function all() {
        $id = $this->input->post('id');
        $doaction = $this->input->post('doaction');
        $this->empty_id_actionall($id);
        $this->empty_id_actionall($doaction, 'message_select_action_from_dropdown');

        switch ($doaction) {
            case 'delete': {
                    $this->cass->delete(array('id' => $id));
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_delete_success'));
                }
                break;
            case 'no_trash': {
                    $data['trash'] = 'NO';
                    $this->cass->save($data, array('id' => $id), TRUE);
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;
            case 'delete_final': {
                    $this->cass->delete(array('id' => $id), FALSE);
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;

            default:
                show_404();
                break;
        }
    }

    public function no_trash($id = false) {
        if ($id) {
            $falid_id = $this->cass->is_found(array('id' => $id, 'trash' => 'YES'), array(), 'row', $this->agent->referrer());
            $data['trash'] = 'NO';
            if ($this->cass->save($data, array('id' => $id, 'trash' => 'YES'), TRUE)) {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function delete_final($id = false) {
        if ($id) {
            $falid_id = $this->cass->is_found(array('id' => $id, 'trash' => 'YES'), array(), 'row', $this->agent->referrer());
            if ($this->cass->delete(array('id' => $id), FALSE)) {
                $this->replay->delete(array('case_id' => $id), FALSE);
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function delete($id = FALSE) {
        if ($id) {
            $falid_id = $this->cass->is_found(array('id' => $id), array(), 'row', __CLASS__);
            if ($this->cass->delete(array('id' => $id))) {
                $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function trash($offset = false) {
        $limit = metohd_get_uri('dblimit', ADMIN_LIMIT);
        $count = $this->cass->filter(TRUE, FALSE, TRUE);
        $this->pass['count'] = $count;
        $this->pass['limit'] = $limit;
        $this->pass['result'] = $this->cass->filter(FALSE, $offset, TRUE);
        $this->pass['pagination'] = $this->admin_pagination('index', $count, $limit);
        $this->themes->render('cases/trash', $this->pass);
    }

    public function add_replay($id = false) {
        if (is_numeric($id)) {
            if ($this->input->post()) {
                $this->load->library('form_validation');
                $this->form_validation->set_rules('comment', lang('label_replay'), 'trim|required');
                if ($this->form_validation->run()) {
                    $save['case_id'] = $id;
                    $save['user_id'] = $this->admin_id;
                    $save['comment'] = $this->input->post('comment');
                    if ($id_replay = $this->replay->save($save)) {
                        $this->pass['replay'] = $replay = $this->replay->get(array('id' => $id_replay), false, 'row');
                        $this->load->view('admin/cases/reply', $this->pass);
                    }
                }
            }
        }
        return false;
    }

    public function edit_replaies($id = false) {
        if ($this->input->post()) {
            if (is_array($this->input->post('editedcomment'))) {
                foreach ($this->input->post('editedcomment') as $key => $value) {
                    $save['comment'] = $value;
                    $this->replay->save($save, array('id' => $key), TRUE);
                }
                return TRUE;
            }
        }
        return false;
    }

    public function close($id = false) {
        if (is_numeric($id)) {
            $this->cass->is_found(array('id' => $id), array(), false, __CLASS__, 'message_cases_canont_close');
            $this->cass->save(array('close' => 'ADMIN'), array('id' => $id), true);
            $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_close_success'));
        }
        redirect($this->agent->referrer());
    }

    public function open($id = false) {
        if (is_numeric($id)) {
            $this->cass->is_found(array('id' => $id), array(), false, __CLASS__, 'message_cases_canont_open');
            $this->cass->save(array('close' => 'NO'), array('id' => $id), true);
            $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_open_success'));
        }
        redirect($this->agent->referrer());
    }

}
